var ns, toast,db;
var app = new Vue({
    el: '#app',
    data: {
        list:[],
    },
    methods: {
        get_data: function () {
            var that = this;
            let selafsql = ` select * from (select PlanType,UploadTime,(SELECT count(ID) as UploadCount FROM Sys_Plan where PlanType=3)as UploadCount ,(SELECT count(ID) as YesUploadCount FROM Sys_Plan where PlanType=3 and IsUpload=1 ) as YesUploadCount  from Sys_Plan where PlanType=3  ORDER BY UploadTime DESC LIMIT 1) a `;
            let selsbsql = ` select * from (select PlanType,UploadTime,(SELECT count(ID) as UploadCount FROM Sys_Plan where PlanType in (1,2))as UploadCount ,(SELECT count(ID) as YesUploadCount FROM Sys_Plan where PlanType in (1,2) and IsUpload=1 ) as YesUploadCount  from Sys_Plan where PlanType in (1,2)  ORDER BY UploadTime DESC LIMIT 1) a `;
            let selplansql=selafsql+' UNION ALL '+selsbsql;
            // let selafsql=`SELECT  PlanType,COUNT(*) AS UploadCount, SUM(CASE WHEN TaskStatus=3 THEN 1 ELSE 0 END) AS YesUploadCount FROM  Sys_Plan GROUP BY  PlanType`;
            // let selplansql=selafsql;
            db.selectSql({
                name: 'prodemotest',
                sql: selplansql
            }, function (ret, err) {
                // console.log('SELECT-统计')
                // console.log(JSON.stringify(ret))
                // console.log(JSON.stringify(err))
                if(ret.status){
                    that.list=ret.data;
                }
                else{
                    that.list=[]; 
                }
            });       
        },
        //清理缓存任务
        onTaskClear: function () {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '将删除所有离线任务，确定?',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                  let res=that.onTaskDelete();
                  if(res){
                      that.get_data();
                  }
                }
                if (ret.buttonIndex == 2) {
                   return
                }     
            });
        },
        onTaskDelete:function(){
             //删除表
             let delconfigsql=`DROP TABLE Sys_ProjectConfig `; 
             let delstandardsql=`DROP TABLE Sys_TaskStandard `; 
             let delfilesql = `DROP TABLE Sys_File `;
             let deltasksql = `DROP TABLE Sys_PlanTask `;
             let delplansql = `DROP TABLE Sys_Plan `;
            //  //删除所有任务数据
            //  let delconfigsql=`DELETE FROM Sys_ProjectConfig `; 
            //  let delstandardsql=`DELETE FROM Sys_TaskStandard `; 
            //  let delfilesql = `DELETE FROM Sys_File `;
            //  let deltasksql = `DELETE FROM Sys_PlanTask `;
            //  let delplansql = `DELETE FROM Sys_Plan `;
            db.executeSql({
                name: 'prodemotest',
                sql: delconfigsql,
            }, function (ret, err) {
            });
             db.executeSql({
                name: 'prodemotest',
                sql: delstandardsql,
            }, function (ret, err) {
            });
             db.executeSql({
                 name: 'prodemotest',
                 sql: delfilesql,
             }, function (ret, err) {
             });
             db.executeSql({
                 name: 'prodemotest',
                 sql: deltasksql,
             }, function (ret, err) {
 
             });
             db.executeSql({
                 name: 'prodemotest',
                 sql: delplansql,
             }, function (ret, err) {
             });
             return true;
        },
        do_opentypelist:function(PlanType){
            let that=this;
            if(PlanType==3){
                ns.openWin('renwulist_offlist', "安防巡更待办(离线模式)");
                return;
            }
            if(PlanType==4){
                // 环境保洁待办(离线模式)
                return;
            }
            ns.openWin('renwulist_equipment_offlist', "设备作业待办(离线模式)");
            return;
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    db = api.require('db');

    //创建prodemotest数据库
    db.openDatabase({
        name: 'prodemotest'
    }, function(ret, err) {
        if (!ret.status) {
            api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
            });
        } 
        else{
            app.get_data();   
        }
    });
    api.addEventListener({
        name: 'home-refresh'
    }, function (ret, err) {
        app.get_data();
    });
}
